package com.ross.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ross.springboot.entity.Role;
import com.ross.springboot.entity.RoleResources;
import com.ross.springboot.mapper.RoleMapper;
import com.ross.springboot.mapper.RoleResourcesMapper;
import com.ross.springboot.service.IRoleService;
import com.ross.springboot.service.impl.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ross
 * @since 2020-07-24
 */
@Service
public class RoleServiceImpl extends BaseService<Role> implements IRoleService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleResourcesMapper roleResourcesMapper;

    @Override
    public List<Role> queryRoleListWithSelected(Integer uid) {
        return this.roleMapper.queryRoleListWithSelected(uid);
    }

    @Override
    public PageInfo<Role> selectByPage(Role role, int start, int length) {
        Integer page = start / length + 1;
        Example example = new Example(Role.class);
        //分页查询
        PageHelper.startPage(page,length);
        List<Role> roleList = selectByExample(example);
        return new PageInfo<>(roleList);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    public void delRole(Integer roleid) {
        //删除角色
        mapper.deleteByPrimaryKey(roleid);
        //删除角色资源
        Example example = new Example(RoleResources.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleid",roleid);
        roleResourcesMapper.deleteByExample(example);
    }
}
